//
//  ProductViewController.m
//  Project two framework
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ProductViewController.h"
#import "FundView.h"
#import "MetalsView.h"
#import "InsureView.h"
#import "ProductTableViewCell.h"
#import "InsureView.h"
#import "PopTView.h"
#import "ZPZNetRequest+FinancialOtherInfo.h"
#import "ZPZNetRequest+MetalsOtherInfo.h"
#import "MJRefresh.h"
#import "SingleView.h"

static NSInteger   indexZ = 0;
static NSInteger   indexG = 0;

@interface ProductViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UISegmentedControl        *segmentedControl;
@property (nonatomic, assign) NSInteger                 lastSelectedIndex;
@property (nonatomic, strong) NSMutableArray            *photoArray;
@property (nonatomic, strong) NSMutableArray            *selectPhoto;
@property (nonatomic, strong) UIImageView               *shadeView;
@property (nonatomic, strong) UIImageView               *bgImage;
@property (nonatomic, strong) FundView                  *fundView;
@property (nonatomic, strong) MetalsView                *meatalView;
@property (nonatomic, strong) InsureView                *insureView;
@property (nonatomic, strong) UILabel                   *textOne;
@property (nonatomic, strong) UILabel                   *textTwo;
@property (nonatomic, strong) UILabel                   *textNine;
@property (nonatomic, strong) UILabel                   *textThree;
@property (nonatomic, strong) UILabel                   *textFour;
@property (nonatomic, strong) UILabel                   *textFive;
@property (nonatomic, strong) UILabel                   *textSix;
@property (nonatomic, strong) UILabel                   *textSeven;
@property (nonatomic, strong) UILabel                   *textEight;
@property (nonatomic, strong) UILabel                   *textTen;
@property (nonatomic, strong) UILabel                   *textElevn;
@property (nonatomic, strong) UILabel                   *textTwelve;
@property (nonatomic, strong) UILabel                   *textThirteen;
@property (nonatomic, strong) UILabel                   *startDate;
@property (nonatomic, strong) UILabel                   *DeadDate;
@property (nonatomic, strong) UIButton                  *investBtn;
@property (nonatomic, strong) UIButton                  *incomeBtn;
@property (nonatomic, strong) UIButton                  *CurrencyBtn;
@property (nonatomic, strong) UITextField               *seachField;
@property (nonatomic, strong) UITableView               *tableView;
@property (nonatomic, strong) UILabel                   *downOne;
@property (nonatomic, strong) UILabel                   *downTwo;
@property (nonatomic, strong) UILabel                   *downThree;
@property (nonatomic, strong) UILabel                   *downFour;
@property (nonatomic, strong) UILabel                   *downFive;
@property (nonatomic, strong) UILabel                   *downSix;
@property (nonatomic, strong) UITextField               *investField;
@property (nonatomic, strong) UIButton                  *countBtn;
@property (nonatomic, strong) UIButton                  *applyBtn;
@property (nonatomic, strong) NSMutableArray            *dataSource;
@property (nonatomic, strong) NSMutableArray            *investArr;
@property (nonatomic, strong) NSMutableArray            *moneyArr;
@property (nonatomic, strong) NSMutableArray            *incomeArr;
@property (nonatomic, strong) NSMutableArray            *timeArr;
/**投资key*/
@property (nonatomic, strong) NSString                  *investStr;
/**收益key*/
@property (nonatomic, strong) NSString                  *incomeStr;
/**钱key*/
@property (nonatomic, strong) NSString                  *moneyStr;
/**搜索提示框*/
@property (nonatomic, strong) UILabel                   *label;
/**当前的indexpath*/
@property (nonatomic, strong) NSIndexPath               *lastIndex;
/**当前点击的tag*/
@property (nonatomic, assign) NSInteger                 currentTag;

@end

@implementation ProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self stepUI];
    [self loadDataSource];
}

- (void)loadDataSource{
    _investStr = @"";
    _incomeStr = @"";
    _moneyStr = @"";
    _lastSelectedIndex = 0;
    _moneyArr = [NSMutableArray array];
    _incomeArr = [NSMutableArray array];
   [ZPZNetRequest getFinancialListPageNum:@"1" pageSize:@"10" currencyId:@"" consignId:@"" startTime:@"" endTime:@"" gainsStyle:@"" keywords:@"" userId:@"22" comletion:^(id object, BOOL success) {
       if (success) {
           _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
           [_tableView reloadData];
       }
   }];
    //币种
    [ZPZNetRequest getFinancialOtherInfoComletion:^(id object, BOOL success) {
        if (success) {
                NSArray *array = object[@"content"][@"currencys"];
                [_moneyArr addObject:@"币种"];
                for (NSDictionary *dic in array) {
                    [_moneyArr addObject:dic[@"name"]];
                }
                _incomeArr = [NSMutableArray arrayWithArray:object[@"content"][@"gainsStyles"]] ;
                [_incomeArr insertObject:@"收益类型" atIndex:0];
            }
        }];
}

- (void)stepUI{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"背景"]];
    [self.view addSubview:self.segmentedControl];
    [self.view addSubview:self.shadeView];
    
    _bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, MAX_Y(_segmentedControl.frame)+20, SCREEN_WIDTH-100, SCREEN_HEIGHT - MAX_Y(_segmentedControl.frame) -  110)];
    _bgImage.userInteractionEnabled = YES;
    _bgImage.image = [UIImage imageNamed:@"down_05"];
    [self.view addSubview:_bgImage];
    
    _fundView = [[FundView alloc] initWithFrame:CGRectMake(30, MAX_Y(_segmentedControl.frame)+30, SCREEN_WIDTH - 140, SCREEN_HEIGHT -  MAX_Y(_segmentedControl.frame)-110)];
    _fundView.userInteractionEnabled = YES;
    
    _insureView = [[InsureView alloc] initWithFrame:CGRectMake(30, MAX_Y(_segmentedControl.frame)+30, SCREEN_WIDTH - 140, SCREEN_HEIGHT -  MAX_Y(_segmentedControl.frame)-110)];
    _insureView.userInteractionEnabled = YES;
    
    _meatalView = [[MetalsView alloc] initWithFrame:CGRectMake(30, MAX_Y(_segmentedControl.frame)+30, SCREEN_WIDTH - 140, SCREEN_HEIGHT -  MAX_Y(_segmentedControl.frame)-110)];
    _meatalView.userInteractionEnabled = YES;
    
    [_bgImage addSubview:self.textOne];
    [_bgImage addSubview:self.startDate];
    [_bgImage addSubview:self.textTwo];
    [_bgImage addSubview:self.DeadDate];
    [_bgImage addSubview:self.investBtn];
    [_bgImage addSubview:self.incomeBtn];
    [_bgImage addSubview:self.CurrencyBtn];
    [_bgImage addSubview:self.seachField];
    [_bgImage addSubview:self.tableView];
    [_bgImage addSubview:self.textThree];
    [_bgImage addSubview:self.textFour];
    [_bgImage addSubview:self.textFive];
    [_bgImage addSubview:self.textSix];
    [_bgImage addSubview:self.textSeven];
    [_bgImage addSubview:self.textEight];
    [_bgImage addSubview:self.textNine];
    [_bgImage addSubview:self.textTen];
    [_bgImage addSubview:self.textElevn];
    [_bgImage addSubview:self.textTwelve];
    [_bgImage addSubview:self.textThirteen];
    
    [_bgImage addSubview:self.downOne];
    [_bgImage addSubview:self.downTwo];
    [_bgImage addSubview:self.investField];
    [_bgImage addSubview:self.downThree];
    [_bgImage addSubview:self.downFour];
    [_bgImage addSubview:self.downFive];
    [_bgImage addSubview:self.downSix];
    [_bgImage addSubview:self.countBtn];
    [_bgImage addSubview:self.applyBtn];
    

    
    //注册财富产品第一页通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataTableView:) name:@"zz" object:nil];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(viewBack) userInfo:nil repeats:YES];
    //注册日历通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabel:) name:@"zpz" object:nil];

}

#pragma mark   ---  自定义方法
- (NSString *)stringWithNumber:(NSNumber *)number{
    NSString *string = [NSString stringWithFormat:@"%@",number];
    return  string;
}

/**弹出键盘,视图上移*/
- (void)showKeyBoard{
    if (_bgImage.frame.origin.y == 80) {
    [UIView animateWithDuration:0.5 animations:^{
        _bgImage.center = CGPointMake(MID_X(_bgImage.frame), MID_Y(_bgImage.frame)-300);
        _segmentedControl.center = CGPointMake(MID_X(_segmentedControl.frame), MID_Y(_segmentedControl.frame)-300);
        _shadeView.center = CGPointMake(MID_X(_shadeView.frame), MID_Y(_shadeView.frame)-300);
          } completion:nil];
    }
}
/**收起键盘,视图还原*/
- (void)hideKeyBoard{
    if (_bgImage.frame.origin.y == -220) {
    [UIView animateWithDuration:0.5 animations:^{
        _segmentedControl.center = CGPointMake(MID_X(self.view.frame)-80, 40);
        _bgImage.frame = CGRectMake(10, MAX_Y(_segmentedControl.frame)+20, SCREEN_WIDTH-100, SCREEN_HEIGHT - MAX_Y(_segmentedControl.frame) -  110);
       _shadeView.center = CGPointMake(MID_X(_segmentedControl.frame), MAX_Y(_segmentedControl.frame)+5);

      } completion:nil];
    }
}


/**处理主视图复原*/
- (void)viewBack{
    if (self.view.center.y != 424) {
        [_investField resignFirstResponder];
        _segmentedControl.center = CGPointMake(MID_X(self.view.frame)-80, 40);
        _bgImage.frame = CGRectMake(10, MAX_Y(_segmentedControl.frame)+20, SCREEN_WIDTH-100, SCREEN_HEIGHT - MAX_Y(_segmentedControl.frame) -  110);
        _shadeView.center = CGPointMake(MID_X(_segmentedControl.frame), MAX_Y(_segmentedControl.frame)+5);    }
}

//刷新
- (void)refreshHomeHeader{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_tableView.mj_header endRefreshing];
    });
}


/**data转换成字符串*/
- (NSString *)dateWithString:(NSDate *)date{
    NSString *string;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    string = [formatter stringFromDate:date];
    return string;
}

///**出现日历*/
//- (void)showcalendar{
//    
//    if (_calendar.frame.size.height == 0) {
//    [UIView animateWithDuration:0.5 animations:^{
//        _calendar.frame = CGRectMake(CGRectGetMinX(_startDate.frame), MAX_Y(_startDate.frame), 250, 300);
//    }];
//    }
//
//}

///**隐藏日历*/
//- (void)hidecalendar{
//    if (_calendar.frame.size.height == 300) {
//        [UIView animateWithDuration:0.5 animations:^{
//            _calendar.frame = CGRectMake(CGRectGetMinX(_startDate.frame), MAX_Y(_startDate.frame), 250, 0);
////            _calendar.alpha = 0;
//            _calendarView.alpha = 0;
//        }];
//    }
//}


#pragma mark   ---  协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndenfier = @"zz";
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndenfier];
    if (!cell) {
        cell = [[ProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"zz"];
    }
    NSDictionary *dic = _dataSource[indexPath.row];
    cell.titleOne.text = dic[@"name"];
    cell.titleTwo.text = dic[@"product_num"];
    cell.titleThree.text = [self stringWithNumber:dic[@"deadline"]];
    cell.titleFour.text = [self stringWithNumber:dic[@"product_yield"]];
    cell.titleFive.text = [self stringWithNumber:dic[@"start_sum"]];
    cell.titleSix.text = dic[@"start_time"];
    cell.titleSeven.text = dic[@"stop_time"];
    cell.titleEight.text = dic[@"carry_interest"];
    cell.titleNine.text = dic[@"expire_time"];
    cell.titleTen.text = [self stringWithNumber:dic[@"product_limit"]];
    if ([[self stringWithNumber:dic[@"iternetbank"]] isEqualToString:@"1"]) {
            cell.titleElevn.text = @"支持";
    }else{
        cell.titleElevn.text = @"不支持";
    }
    return cell;
}

-(void)textFieldDidChange :(UITextField *)theTextField{
    if (_seachField == theTextField) {
        [ZPZNetRequest getFinancialListPageNum:@"1" pageSize:@"10" currencyId:@"" consignId:@"" startTime:@"" endTime:@"" gainsStyle:@"" keywords:_seachField.text userId:@"22" comletion:^(id object, BOOL success) {
            if (success) {
                _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
                if (_dataSource.count > 0) {
                    [_tableView reloadData];
                    [_label removeFromSuperview];
                }else{
                    [self label];
                }
            }
        }];
    }else{
        [self label];
    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.bgImage.image = [UIImage imageNamed:@"选中new"];
    if (_lastIndex != nil && indexPath != _lastIndex) {
        ProductTableViewCell *cell = [tableView cellForRowAtIndexPath:_lastIndex];
        cell.bgImage.image = [UIImage imageNamed:@"line_15"];
    }
    _lastIndex = indexPath;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == _investField) {
        //键盘弹出通知
        [self showKeyBoard];
    }
    return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_investField resignFirstResponder];
    [self hideKeyBoard];
    return YES;
}

#pragma mark   ---  通知
- (void)reloadDataTableView:(NSNotification *)sender{
    indexZ = 0;
    indexG = 0;
    NSArray *currencyID = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
    NSArray *currenID = @[@"0",@"4",@"2",@"1",@"4",@"5",@"6",@"7"];
    NSLog(@"%@",sender.userInfo);
    for (NSString *string in _incomeArr) {
        if ([string isEqualToString:sender.userInfo[@"name"]]) {
            [_incomeBtn setTitle:string forState:UIControlStateNormal];
            _incomeStr = string;
        }
    }
    for (NSString *string in _timeArr) {
        if ([string isEqualToString:sender.userInfo[@"name"]]) {
            [_investBtn setTitle:string forState:UIControlStateNormal];
            _investStr = [NSString stringWithFormat:@"%@",currencyID[indexZ]];;
        }
        indexZ++;
    }
    for (NSString *string in _moneyArr) {
        if ([string isEqualToString:sender.userInfo[@"name"]]) {
            [_CurrencyBtn setTitle:string forState:UIControlStateNormal];
            _moneyStr = [NSString stringWithFormat:@"%@",currenID[indexG]];;
        }
        indexG++;
    }
    [ZPZNetRequest getFinancialListPageNum:@"1" pageSize:@"10" currencyId:_moneyStr consignId:_investStr startTime:@"" endTime:@"" gainsStyle:@"" keywords:@"" userId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource = [NSMutableArray arrayWithArray:object[@"content"]];
            if (_dataSource.count > 0) {
                [_tableView reloadData];
                [_label removeFromSuperview];
//                NSLog(@"%@",_label);
            }else{
                [self label];
            }
        }else{
            [self label];
        }
    }];
    
}

- (void)changeLabel:(NSNotification *)sender{
    NSString *string = [self dateWithString:sender.userInfo[@"date"]];
    if (_currentTag == 300) {
        _startDate.text = string;
    }else{
        _DeadDate.text = string;
    }
}



#pragma mark   ---  点击事件
- (IBAction)segmentedControlStyle:(UISegmentedControl *)sender
{
    [_segmentedControl setImage:[_photoArray[_lastSelectedIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:_lastSelectedIndex];
    switch([sender selectedSegmentIndex])
    {
        case 0:
            [_segmentedControl setImage:[_selectPhoto[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
            //弹出当前视图
            [self.view addSubview:_bgImage];
            //移除其他视图
            [_fundView removeFromSuperview];
            [_insureView removeFromSuperview];
            [_meatalView removeFromSuperview];
            break;
        case 1:
            [_segmentedControl setImage:[_selectPhoto[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
            //弹出当前视图
            [self.view addSubview:_fundView];
            //移除上一个视图
            [_bgImage removeFromSuperview];
            [_insureView removeFromSuperview];
            [_meatalView removeFromSuperview];
            break;
        case 2:
            [_segmentedControl setImage:[_selectPhoto[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:2];
            [self.view addSubview:_meatalView];
            
            [_fundView removeFromSuperview];
            [_bgImage removeFromSuperview];
            [_insureView removeFromSuperview];
            break;
        case 3:
            [_segmentedControl setImage:[_selectPhoto[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:3];
            [self.view addSubview:_insureView];
            [_bgImage removeFromSuperview];
            [_meatalView removeFromSuperview];
            [_fundView removeFromSuperview];
            break;
    }
    _lastSelectedIndex = _segmentedControl.selectedSegmentIndex;
}

- (void)investBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    _timeArr = [NSMutableArray arrayWithArray:@[@"投资期限",@"一个月以内",@"1-3个月",@"3-6个月",@"6-12个月",@"1-3年",@"3-5年",@"5年以上"]];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+90, MAX_Y(sender.frame)+295) DataSource:_timeArr]];
}

- (void)incomeBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+90, MAX_Y(sender.frame)+295) DataSource:_incomeArr]];
}

- (void)currencyBtnTap:(UIButton *)sender{
    PopTView *pop = [[PopTView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:pop];
    [window addSubview:[PopTView sharePopView:CGPointMake(sender.center.x+90, MAX_Y(sender.frame)+295) DataSource:_moneyArr]];
}

//计算按钮
- (void)countBtnTap:(UIButton *)sender{
    //投资金额
    if (_lastIndex != nil) {
        CGFloat investMoney = [_investField.text floatValue];
        ProductTableViewCell *cell = [_tableView cellForRowAtIndexPath:_lastIndex];
        //投资日期
        CGFloat deadLine = [cell.titleThree.text floatValue];
        //预期收益率
        CGFloat rate = [cell.titleFour.text floatValue];
        //收益
        CGFloat deadlineO= deadLine/365.00;
        CGFloat income = deadlineO * rate * investMoney;
        NSString *incomeStr = [NSString stringWithFormat:@"%.4f",income];
        _downFive.text = incomeStr;
    }else{
        [self creatAlectControllerTitle:@"请选择产品" message:@"" oneTitle:@"确定" twoTitle:nil];
    }
    
    if (_bgImage.frame.origin.y == MAX_Y(_segmentedControl.frame)+20) {
        [_investField resignFirstResponder];
        [self hideKeyBoard];
    }
   
}

- (void)applyBtnTap:(UIButton *)sender{
    [self hideKeyBoard];
    if (_lastIndex == nil) {
        [self creatAlectControllerTitle:@"请选择产品" message:@"" oneTitle:@"确定" twoTitle:nil];
    }else{
        ProductTableViewCell *cell = [_tableView cellForRowAtIndexPath:_lastIndex];
        NSString *startTime = cell.titleSix.text;
        NSString *endTime = cell.titleSeven.text;
        NSDate *date = [NSDate date];

        NSDate *date1 = [self stringWithDate:startTime];
        NSDate *date2 = [self stringWithDate:endTime];
        NSTimeInterval current =  [self timeWithDate:date];
   NSTimeInterval start =  [self timeWithDate:date1];
   NSTimeInterval end =  [self timeWithDate:date2];
        if (start <= current && current <= end) {
            [SingleView showOtherView];
        }else{
        [self creatAlectControllerTitle:@"提示" message:@"该产品不在购买期内!" oneTitle:@"确定" twoTitle:nil];
        }
    }
}

#pragma mark   ---  手势
//- (void)startTime{
//    _currentTag = 300;
//    _startDate.layer.borderColor = [UIColor colorWithRed:187/255.0 green:44/255.0 blue:45/255.0 alpha:1].CGColor;
//    _startDate.layer.borderWidth = 2.0;
//    _calendar.dataSource = self;
//    _calendar.delegate = self;
//    [self endBackColor];
//    [self.bgImage addSubview:_calendarView];
//    [self.bgImage addSubview:_calendar];
//    [self showcalendar];
//}

//- (void)endTime{
//    _currentTag = 301;
//    _DeadDate.layer.borderColor = [UIColor colorWithRed:187/255.0 green:44/255.0 blue:45/255.0 alpha:1].CGColor;
//    _DeadDate.layer.borderWidth = 2.0;
//    [self showcalendar];
//    _calendar.dataSource = self;
//    _calendar.delegate = self;
//    [self startBackColor];
//    [self.bgImage addSubview:_calendarView];
//    [self.bgImage addSubview:_calendar];
//}

- (void)startBackColor{
    _startDate.layer.borderWidth = 0;
}

- (void)endBackColor{
    _DeadDate.layer.borderWidth = 0;
}

//- (void)hideCalendarView{
//    [self hidecalendar];
//}


#pragma mark   ---  懒加载

- (UISegmentedControl *)segmentedControl{
    _photoArray = [NSMutableArray array];
    _selectPhoto = [NSMutableArray array];
    if (!_segmentedControl) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"1_08"]];
        UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:@"2_09"]];
        UIImage *image2 = [UIImage imageNamed:[NSString stringWithFormat:@"3_10"]];
        UIImage *image3 = [UIImage imageNamed:[NSString stringWithFormat:@"4_11"]];
        [_photoArray addObject:image];
        [_photoArray addObject:image1];
        [_photoArray addObject:image2];
        [_photoArray addObject:image3];
        
        UIImage *image4 = [UIImage imageNamed:[NSString stringWithFormat:@"理财产品_08"]];
        UIImage *image5 = [UIImage imageNamed:[NSString stringWithFormat:@"基金_09"]];
        UIImage *image6 = [UIImage imageNamed:[NSString stringWithFormat:@"贵金属_10"]];
        UIImage *image7 = [UIImage imageNamed:[NSString stringWithFormat:@"保险_11"]];
        [_selectPhoto addObject:image4];
        [_selectPhoto addObject:image5];
        [_selectPhoto addObject:image6];
        [_selectPhoto addObject:image7];
        
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"",@"",@"",@""]];
        _segmentedControl.center = CGPointMake(MID_X(self.view.frame)-80, 40);
        _segmentedControl.bounds = CGRectMake(0, 0, 480, 40);
        for (NSInteger i = 0; i < 4; i++) {
            if (i == 0) {
                [_segmentedControl setImage:[_selectPhoto[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:i
                 ];
            }else{
                [_segmentedControl setImage:[_photoArray[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:i
                 ];
            }
        }
        [_segmentedControl addTarget:self action:@selector(segmentedControlStyle:) forControlEvents:(UIControlEventValueChanged)];
        _segmentedControl.tintColor = [UIColor clearColor];
    }
    return _segmentedControl;
}

- (UIImageView *)shadeView{
    if (!_shadeView) {
        _shadeView = [[UIImageView alloc] init];
        _shadeView.center = CGPointMake(MID_X(_segmentedControl.frame), MAX_Y(_segmentedControl.frame)+5);
        _shadeView.bounds = CGRectMake(0, 0, CGRectGetWidth(_segmentedControl.bounds), 10);
        _shadeView.image = [UIImage imageNamed:@"shadow_08"];
    }
    return _shadeView;
}

- (UILabel *)textOne{
    if (!_textOne) {
        _textOne = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 80, 20)];
        _textOne.text = @"起售起始日:";
        _textOne.textColor = WHITE_C;
        _textOne.font = [UIFont systemFontOfSize:15];
    }
    return _textOne;
}

- (UILabel *)startDate{
    if (!_startDate) {
        _startDate = [[UILabel alloc] init];
        _startDate.center = CGPointMake(MAX_X(_textOne.frame) +65, MID_Y(_textOne.frame));
        _startDate.bounds = CGRectMake(0, 0, 110, 35);
        _startDate.backgroundColor = WHITE_C;
//        UITapGestureRecognizer  *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startTime)];
        _startDate.userInteractionEnabled = YES;
//        [_startDate addGestureRecognizer:tapGes];
    }
    return _startDate;
}

- (UILabel *)textTwo{
    if (!_textTwo) {
        _textTwo = [[UILabel alloc] init];
        _textTwo.center = CGPointMake(MAX_X(_startDate.frame)+15, MID_Y(_textOne.frame));
        _textTwo.bounds = CGRectMake(0, 0, 15, 20);
        _textTwo.text = @"到";
        _textTwo.textAlignment = NSTextAlignmentCenter;
        _textTwo.textColor = WHITE_C;
        _textTwo.font = [UIFont systemFontOfSize:15];

    }
    return _textTwo;
}

- (UILabel *)DeadDate{
    if (!_DeadDate) {
        _DeadDate = [[UILabel alloc] init];
        _DeadDate.center = CGPointMake(MAX_X(_textTwo.frame) +65, MID_Y(_textOne.frame));
        _DeadDate.userInteractionEnabled = YES;
        _DeadDate.bounds = CGRectMake(0, 0, 110, 35);
        _DeadDate.backgroundColor = WHITE_C;
//                UITapGestureRecognizer  *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endTime)];
//        [_DeadDate addGestureRecognizer:tapGes];
    }
    return _DeadDate;
}

- (UIButton *)investBtn{
    if (!_investBtn) {
        _investBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _investBtn.center = CGPointMake(MAX_X(_DeadDate.frame)+60, MID_Y(_textOne.frame));
        _investBtn.bounds = CGRectMake(0, 0, 80, 35);
        _investBtn.backgroundColor = WHITE_C;
        [_investBtn setTitle:@"投资期限" forState:UIControlStateNormal];
        [_investBtn setTitleColor:BLACK_C forState:UIControlStateNormal];
        [_investBtn addTarget:self action:@selector(investBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _investBtn;
}

- (UIButton *)incomeBtn{
    if (!_incomeBtn) {
        _incomeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _incomeBtn.center = CGPointMake(MAX_X(_investBtn.frame)+60, MID_Y(_textOne.frame));
        _incomeBtn.bounds = CGRectMake(0, 0, 80, 35);
        _incomeBtn.backgroundColor = WHITE_C;
        [_incomeBtn setTitle:@"收益类型" forState:UIControlStateNormal];
        [_incomeBtn setTitleColor:BLACK_C forState:UIControlStateNormal];
        [_incomeBtn addTarget:self action:@selector(incomeBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _incomeBtn;
}

- (UIButton *)CurrencyBtn{
    if (!_CurrencyBtn) {
        _CurrencyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _CurrencyBtn.center = CGPointMake(MAX_X(_incomeBtn.frame)+50, MID_Y(_textOne.frame));
        _CurrencyBtn.bounds = CGRectMake(0, 0, 60, 35);
        _CurrencyBtn.backgroundColor = WHITE_C;
        [_CurrencyBtn setTitle:@"币种" forState:UIControlStateNormal];
        [_CurrencyBtn setTitleColor:BLACK_C forState:UIControlStateNormal];
        [_CurrencyBtn addTarget:self action:@selector(currencyBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _CurrencyBtn;
}

- (UITextField *)seachField{
    if (!_seachField) {
        _seachField = [[UITextField alloc] init];
        _seachField.center = CGPointMake(MAX_X(_CurrencyBtn.frame) +140, MID_Y(_textOne.frame));
        _seachField.backgroundColor = WHITE_C;
        _seachField.placeholder = @"   Seach";
        _seachField.bounds = CGRectMake(0, 0, 220, 35);
        _seachField.layer.cornerRadius = 220/12;
        _seachField.clipsToBounds = YES;
        //编辑时会出现个修改X
        _seachField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_seachField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _seachField;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, MAX_Y(_seachField.frame)+ 60, CGRectGetWidth(_bgImage.frame)-40, 360) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 45;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshHomeHeader];
        }];
    }
    return _tableView;
}

- (UILabel *)textThree{
    if (!_textThree) {
        _textThree = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, MIN_Y(_tableView.frame)-30, 30, 20)];
            label.text = @"产品";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });
    }
    return _textThree;
}

- (UILabel *)textFour{
    if (!_textFour) {
        _textFour = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textThree.frame)+110, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 30, 20);
            label.text = @"代码";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });

    }
    return _textFour;
}

- (UILabel *)textFive{
    if (!_textFive) {
        _textFive = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textFour.frame)+90, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 60, 40);
            label.text = @"投资期限(天)";
            label.numberOfLines = 2;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12 weight:2];
            label.textColor = WHITE_C;
            label;
        });

    }
    return _textFive;
}

- (UILabel *)textSix{
    if (!_textSix) {
        _textSix = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textFive.frame)+30, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 45, 40);
            label.text = @"预期收益率(%)";
            label.numberOfLines = 2;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12 weight:2];
            label.textColor = WHITE_C;
            label;
        });

    }
    return  _textSix;
}

- (UILabel *)textSeven{
    if (!_textSeven) {
        _textSeven = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textSix.frame)+30, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 50, 40);
            label.text = @"起点金额(万元)";
            label.numberOfLines = 2;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12 weight:2];
            label.textColor = WHITE_C;
            label;
        });
    }
    return _textSeven;
}

- (UILabel *)textEight{
    if (!_textEight) {
        _textEight = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textSeven.frame)+50, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 40, 20);
            label.text = @"起售日";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });
    }
    return _textEight;
}

- (UILabel *)textNine{
    if (!_textNine) {
        _textNine = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textEight.frame)+75, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 45, 20);
            label.text = @"停售日";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });
    }
    return _textNine;
}

- (UILabel *)textTen{
    if (!_textTen) {
        _textTen = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textNine.frame)+75, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 45, 20);
            label.text = @"起息日";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });
    }
    return _textTen;
}

- (UILabel *)textElevn{
    if (!_textElevn) {
        _textElevn = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textTen.frame)+65, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 45, 20);
            label.text = @"到期日";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });

    }
    return _textElevn;
}

- (UILabel *)textTwelve{
    if (!_textTwelve) {
        _textTwelve = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_textElevn.frame)+50, MID_Y(_textThree.frame));
            label.bounds = CGRectMake(0, 0, 40, 40);
            label.text = @"额度(亿元)";
            label.numberOfLines = 2;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = WHITE_C;
            label;
        });
        
    }
    return _textTwelve;
}

- (UILabel *)textThirteen{
    if (!_textThirteen) {
         _textThirteen = ({
        UILabel *label = [[UILabel alloc] init];
        label.center = CGPointMake(MAX_X(_textTwelve.frame)+30, MID_Y(_textThree.frame));
        label.bounds = CGRectMake(0, 0, 30, 20);
        label.text = @"网银";
        label.numberOfLines = 2;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:13 weight:2];
        label.textColor = WHITE_C;
        label;
         });
    }
    return _textThirteen;
}

- (UILabel *)downOne{
    if (!_downOne) {
        _downOne = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(70, MAX_Y(_tableView.frame)+30, 100, 20)];
            label.text = @"预期收益计算:";
            label.numberOfLines = 2;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:15 weight:3];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downOne;
}

- (UILabel *)downTwo{
    if (!_downTwo) {
        _downTwo = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_downOne.frame)+40, MID_Y(_downOne.frame));
            label.bounds = CGRectMake(0, 0, 60, 20);
            label.text = @"投资金额";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downTwo;
}

- (UITextField *)investField{
    if (!_investField) {
        _investField = ({
            UITextField *textField = [[UITextField alloc] init];
            textField.textAlignment = NSTextAlignmentCenter;
            textField.center = CGPointMake(MAX_X(_downTwo.frame)+70, MID_Y(_downOne.frame));
            textField.font = [UIFont systemFontOfSize:15];
            textField.textColor = [UIColor grayColor];
            textField.backgroundColor = WHITE_C;
            textField.bounds = CGRectMake(0, 0, 120, 35);
            textField.delegate = self;
            textField;
        });
    }
    return _investField;
}

- (UILabel *)downThree{
    if (!_downThree) {
        _downThree = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_investField.frame)+20, MID_Y(_downOne.frame));
            label.bounds = CGRectMake(0, 0, 30, 20);
            label.text = @"万元";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downThree;
}

- (UILabel *)downFour{
    if (!_downFour) {
        _downFour = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_downThree.frame)+70, MID_Y(_downOne.frame));
            label.bounds = CGRectMake(0, 0, 90, 20);
            label.text = @"预期投资收益:";
            label.numberOfLines = 1;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downFour;
}

- (UILabel *)downFive{
    if (!_downFive) {
        _downFive = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_downFour.frame)+70, MID_Y(_downOne.frame));
            label.bounds = CGRectMake(0, 0, 120, 35);
            label.numberOfLines = 1;
            label.backgroundColor = WHITE_C;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:15];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downFive;
}

- (UILabel *)downSix{
    if (!_downSix) {
        _downSix = ({
            UILabel *label = [[UILabel alloc] init];
            label.center = CGPointMake(MAX_X(_downFive.frame)+20, MID_Y(_downOne.frame));
            label.bounds = CGRectMake(0, 0, 30, 20);
            label.numberOfLines = 1;
            label.text = @"万元";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:13 weight:2];
            label.textColor = [UIColor grayColor];
            label;
        });
    }
    return _downSix;
}

- (UIButton *)countBtn{
    if (!_countBtn) {
        _countBtn = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.center = CGPointMake(MAX_X(_downSix.frame)+70, MID_Y(_downOne.frame));
            btn.bounds = CGRectMake(0, 0, 90, 40);
            [btn setImage:[UIImage imageNamed:@"计算_01"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(countBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            btn;
        });
    }
    return _countBtn;
}

- (UIButton *)applyBtn{
    if (!_applyBtn) {
        _applyBtn = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.center = CGPointMake(MID_X(self.view.frame) - 60, MAX_Y(_investField.frame)+35);
            btn.bounds = CGRectMake(0, 0, 150, 50);
            [btn setImage:[UIImage imageNamed:@"申请"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(applyBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
            btn;
        });
    }
    return _applyBtn;
}

- (UILabel *)label{
    if (!_label) {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(300, 140, 300, 200)];
      _label.text = @"没有符合您搜索条件的数据";
      _label.font = [UIFont systemFontOfSize:18 weight:2];
      _label.textColor = WHITE_C;
      _label.textAlignment = NSTextAlignmentCenter;
      _dataSource = [NSMutableArray array];
      [_tableView reloadData];
     [_bgImage addSubview:_label];
    }
    return _label;
}
@end